package com.stm.basicData.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stm.basicData.mapper.ToolsTestOptionMapper;
import com.stm.basicData.domain.KfToolsTestProject;
import com.stm.basicData.domain.ToolsTestOption;
import com.stm.basicData.service.IToolsTestOptionService;
import com.stm.basicData.service.KfToolsTestProjectService;
import com.stm.common.annotation.DataScope;
import com.stm.common.core.text.Convert;
import com.stm.common.utils.DateUtils;

import org.springframework.transaction.annotation.Transactional;

/**
 * 工具试验 服务层实现
 * 
 * @author wyc
 * @date 2019-04-08
 */
@Service
public class ToolsTestOptionServiceImpl implements IToolsTestOptionService 
{
	@Autowired
	private ToolsTestOptionMapper toolsTestOptionMapper;
	@Autowired
	private KfToolsTestProjectService kfToolsTestProjectService;

	/**
     * 查询工具试验信息
     * 
     * @param id 工具试验ID
     * @return 工具试验信息
     */
    @Override
	public ToolsTestOption selectToolsTestOptionById(String id)
	{
	    return toolsTestOptionMapper.selectToolsTestOptionById(id);
	}
	
	/**
     * 查询工具试验列表
     * 
     * @param toolsTestOption 工具试验信息
     * @return 工具试验集合
     */
	@Override
	@DataScope(tableAlias = "b3")
	public List<ToolsTestOption> selectToolsTestOptionList(ToolsTestOption toolsTestOption)
	{
	    return toolsTestOptionMapper.selectToolsTestOptionList(toolsTestOption);
	}
	
    /**
     * 新增工具试验
     * 
     * @param toolsTestOption 工具试验信息
     * @return 结果
     */
	@Override
	public int insertToolsTestOption(ToolsTestOption toolsTestOption)
	{
		UUID uuid = UUID.randomUUID();
		toolsTestOption.setId(uuid.toString());
		toolsTestOption.setStatus("1");
		toolsTestOption.setCreateDate(new Date());
		
		for (KfToolsTestProject kfToolsTestProject : toolsTestOption.getKfToolsTestProjectList()) {
			
			UUID uuid1 = UUID.randomUUID();
	    	kfToolsTestProject.setId(uuid1.toString());
	    	kfToolsTestProject.setCreateDate(DateUtils.getNowDate());
			kfToolsTestProject.setOptionId(uuid.toString());
			
			kfToolsTestProjectService.insertKfToolsTestProject(kfToolsTestProject); //新增试验项目
		}
		
		
		return toolsTestOptionMapper.insertToolsTestOption(toolsTestOption);
	}
	
	/**
     * 修改工具试验
     * 
     * @param toolsTestOption 工具试验信息
     * @return 结果
	 * @throws Exception 
     */
	@Override
	@Transactional
	public int updateToolsTestOption(ToolsTestOption toolsTestOption) throws Exception
	{
		toolsTestOption.setUpdateDate(new Date());
		
		
		kfToolsTestProjectService.deleteKfToolsTestProjectByOptionId(toolsTestOption.getId());//先删除所在id的数据
		
		for (KfToolsTestProject kfToolsTestProject : toolsTestOption.getKfToolsTestProjectList()) {
			
			UUID uuid1 = UUID.randomUUID();
	    	kfToolsTestProject.setId(uuid1.toString());
	    	kfToolsTestProject.setCreateDate(DateUtils.getNowDate());
			kfToolsTestProject.setOptionId(toolsTestOption.getId());
			
			kfToolsTestProjectService.insertKfToolsTestProject(kfToolsTestProject); //新增试验项目
		}
		
	    return toolsTestOptionMapper.updateToolsTestOption(toolsTestOption);
	}

	/**
     * 删除工具试验对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
	 * @throws Exception 
     */
	@Override
	@Transactional
	public int deleteToolsTestOptionByIds(String ids) throws Exception
	{
		
		String[] KfToolsBoxIds = Convert.toStringArray(ids);
		
		for (String id : KfToolsBoxIds) {
			kfToolsTestProjectService.deleteKfToolsTestProjectByOptionId(id);//先删除所在id的数据
		}
		
		return toolsTestOptionMapper.deleteToolsTestOptionByIds(KfToolsBoxIds);
	}

	@Override
	public List<ToolsTestOption> toolsTestProjectList(String ids) {
		String[] kfToolsIds = Convert.toStringArray(ids);
		return toolsTestOptionMapper.toolsTestProjectList(kfToolsIds);
	}
	
}
